<template>
	<view class="bgMinor2 pt-24 plr-24 pt-24" :style="{ minHeight: `${windowHeight}px`, height: 'auto' }">
		<view>
			<view class="pl-24 mb-24 size-34 color-black2">品牌信息</view>
			<view class="bgWhite plr-24 radius-16">
				<view class="flex ptb-24 border-b-2">
					<view class="size-28 width-160">LOGO</view>
					<view class="flex-1">

						<view class="center radius-16" style="background: #ffffff;width:30%;overflow: hidden;"
							@click="uploadImage">
							<view class="radius-16 center"
								style="width:100%; height:233rpx;padding: 0;position: relative;">
								<img v-if="dataInfo.referImg" @click.stop="dataInfo.referImg = ''"
									src="@/static/images/del.png" style="cursor: pointer;position: absolute; top: 6px;
					z-index: 32 !important;
					right:   6px;width: 20px;height: 20px;">
								<img v-if="dataInfo.referImg !=''  &&dataInfo.referImg != null" mode="aspectFit"
									style="width: 100%;height: 233rpx !important" :src="dataInfo.referImg"></img>
								<view v-if="dataInfo.referImg =='' ||dataInfo.referImg == null" class=" "
									style="width: 120%;height: 153rpx !important;display: flex;flex-direction: column;justify-content: center; align-items: center;background: #f4f5f7;">

									<img src="@/static/images/camera.png" style='width: 50rpx;height:50rpx;'>
								</view>
							</view>
							<view></view>
						</view>

					</view>
				</view>
				<view class="flex ptb-24 border-b-2">
					<view class="size-28 width-160">品牌名称</view>
					<view class="flex-1">
						<uv-input placeholder="点击输入品牌名称" border="none" v-model="dataInfo.ppmc"></uv-input>
					</view>
				</view>
				<view class="flex ptb-24 border-b-2">
					<view class="size-28 width-160">联系方式</view>
					<view class="flex-1">
						<uv-input placeholder="点击输入手机号" border="none" v-model="dataInfo.phone"></uv-input>
					</view>
				</view>
			</view>
		</view>

		<view class="mt-30">
			<view class="pl-24 mb-24 size-34 color-black2">主体资质</view>
			<view class="bgWhite plr-24 radius-16">
				<view class="flex ptb-24 border-b-2">
					<view class="size-28 width-160">营业执照</view>
					<view class="flex-1">
						<view class="flex-1">

							<view class="center radius-16" style="background: #ffffff;width:30%;overflow: hidden;"
								@click="uploadImage1">
								<view class="radius-16 center"
									style="width:100%; height:233rpx;padding: 0;position: relative;">
									<img v-if="dataInfo.referImg1" @click.stop="dataInfo.referImg1 = ''"
										src="@/static/images/del.png" style="cursor: pointer;position: absolute; top: 6px;
					z-index: 32 !important;
					right:   6px;width: 20px;height: 20px;">
									<img v-if="dataInfo.referImg1 !=''  &&dataInfo.referImg1 != null" mode="aspectFit"
										style="width: 100%;height: 233rpx !important" :src="dataInfo.referImg1"></img>
									<view v-if="dataInfo.referImg1 =='' ||dataInfo.referImg1 == null" class=" "
										style="width: 120%;height: 153rpx !important;display: flex;flex-direction: column;justify-content: center; align-items: center;background: #f4f5f7;">

										<img src="@/static/images/camera.png" style='width: 50rpx;height:50rpx;'>
									</view>
								</view>
								<view></view>
							</view>

						</view>
					</view>
				</view>
				<view class="flex ptb-24 border-b-2">
					<view class="size-28 width-160">企业名称</view>
					<view class="flex-1">
						<uv-input placeholder="需与证件保持一致" border="none" v-model="dataInfo.enterpriseName"></uv-input>
					</view>
				</view>
				<view class="flex ptb-24 border-b-2">
					<view class="size-28 width-160 lineHeight1">
						<view style="margin-bottom: 30rpx;">统一社会</view>
						<view>信用代码</view>
					</view>
					<view class="flex-1">
						<uv-input placeholder="需与证件保持一致" border="none" v-model="dataInfo.unifiedCode"></uv-input>
					</view>
				</view>
			</view>
		</view>

		<view class="color-blue1 size-24 mt-24">重新认证</view>

		<view class="mt-30">
			<view class="width100 height-90"></view>
			<view class="fixedClass plr-24">
				<view class="list_b_btn" @click="onSubmit">认证成功</view>
			</view>
		</view>
	</view>
</template>

<script setup lang="ts">
	import { reactive } from 'vue';
	import { get, post, baseConfig } from '@/utils/request';
	import { getWindowHeight } from '@/pages/util/getHeight';
	import { onLoad } from '@dcloudio/uni-app';
	import { addOpen, loggedIn, addOpenTab } from '@/pages/util/hooks';
	import useLoginStore from '@/pinia-store/userInfoStore';
	const useLogin = useLoginStore();
	// console.log('useLoginuseLogin',useLogin);
	const windowHeight = getWindowHeight();
	console.log('aaaaaaaaaaaaaa', windowHeight);
	/** @name 数据 */
	const dataInfo = reactive({
		username: '',
		fileList1: [],
		fileList2: []
	});
	onLoad((options : any) => { });

	async function uploadImage1() {

		uni.chooseImage({
			count: 1, // 默认9
			sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
			sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
			success: (res) => {
				console.log('上传文件')
				console.log(res.tempFiles)
				uni.uploadFile({
					url: baseConfig.baseURL + 'sys/common/upload', // 替换为实际的上传接口地址
					filePath: res.tempFilePaths[0],
					name: 'file',
					header: {
						'X-Access-Token': uni.getStorageSync('Token')
					},
					formData: {
						// 这里可以添加其他表单数据
						file: res.tempFiles[0],
					},
					success: (uploadRes) => {
						let data = JSON.parse(uploadRes.data)
						dataInfo.referImg1 = data.message
						console.log(dataInfo.referImg)
						console.log('上传成功', data);
					},
					fail: (err) => {
						console.error('上传失败', err);
					}
				});
			}
		});
	}

	async function uploadImage() {

		uni.chooseImage({
			count: 1, // 默认9
			sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
			sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
			success: (res) => {
				console.log('上传文件')
				console.log(res.tempFiles)
				uni.uploadFile({
					url: baseConfig.baseURL + 'sys/common/upload', // 替换为实际的上传接口地址
					filePath: res.tempFilePaths[0],
					name: 'file',
					header: {
						'X-Access-Token': uni.getStorageSync('Token')
					},
					formData: {
						// 这里可以添加其他表单数据
						file: res.tempFiles[0],
					},
					success: (uploadRes) => {
						let data = JSON.parse(uploadRes.data)
						dataInfo.referImg = data.message
						console.log(dataInfo.referImg)
						console.log('上传成功', data);
					},
					fail: (err) => {
						console.error('上传失败', err);
					}
				});
			}
		});
	}
	/** @name xxx */
	function onSubmit() {
		let data = {
			"logo": dataInfo.referImg,
			"ppmc":  dataInfo.ppmc,
			"phone":  dataInfo.phone,
			"businessLicense": dataInfo.referImg1,
			"enterpriseName": dataInfo.enterpriseName,
			"unifiedCode":  dataInfo.unifiedCode,
		}
		fetchData(data)


	}
	
	const fetchData = async (data) => {
		try {
			const response = await post<{
				"logo": "String"
				"ppmc":  "String"
				"phone":   "String"
				"businessLicense":  "String"
				"enterpriseName":  "String"
				"unifiedCode":  "String"
			}>('/merchantAuth/add',data);
	
	
			dataInfo.proportion = response.result.proportion
			dataInfo.light = response.result.special.light
			dataInfo.background = response.result.special.background
			dataInfo.angle = response.result.special.angle
			dataInfo.portrait = response.result.special.portrait
			dataInfo.style = type == 'c' ? response.result.style : response.result.tstStyle
			console.log(response.result);
		} catch (error) {
			console.error(error);
		}
	};

	// 删除图片
	function deletePic(event) {
		dataInfo[`fileList${event.name}`].splice(event.index, 1);
	}

	// 新增图片
	async function afterRead(event) {
		console.log('新增图片event', event);
		// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
		let lists = [].concat(event.file);
		let fileListLen = dataInfo[`fileList${event.name}`].length;
		lists.map((item) => {
			dataInfo[`fileList${event.name}`].push({
				...item,
				status: 'uploading',
				message: '上传中'
			});
		});
		for (let i = 0; i < lists.length; i++) {
			const result = await uploadFilePromise(lists[i].url);
			let item = dataInfo[`fileList${event.name}`][fileListLen];
			dataInfo[`fileList${event.name}`].splice(
				fileListLen,
				1,
				Object.assign(item, {
					status: 'success',
					message: '',
					url: result
				})
			);
			fileListLen++;
		}
	}
	function uploadFilePromise(url) {
		return new Promise((resolve, reject) => {
			let a = uni.uploadFile({
				url: 'http://192.168.2.21:7001/upload', // 仅为示例，非真实的接口地址
				filePath: url,
				name: 'file',
				formData: {
					user: 'test'
				},
				success: (res) => {
					setTimeout(() => {
						resolve(res.data);
					}, 1000);
				}
			});
		});
	}
</script>

<style scoped lang="scss">
	// :deep(.checklist-box) {
	// 	//  模版
	// }

	:deep(.uv-upload__button) {
		margin: 0;
	}

	// 按钮
	.list_b_btn {
		background: -webkit-linear-gradient(left top, #ffc800, #ff0053, #ff5037); // , 100%,#ff5037 left top,
	}
</style>